// 云函数入口文件
const cloud = require("wx-server-sdk");

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
});

const db = cloud.database();
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  const { userId, userInfo } = event;
  const { OPENID } = cloud.getWXContext();

  try {
    // 更新用户信息
    const result = await db
      .collection("users")
      .doc(userId)
      .update({
        data: {
          ...userInfo,
          updateTime: db.serverDate(),
        },
      });

    return {
      code: 0,
      msg: "更新成功",
      data: result,
    };
  } catch (err) {
    console.error(err);
    return {
      code: -1,
      msg: "更新失败",
      error: err,
    };
  }
};
